<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/2/9
  Time: 12:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="/css/style.min.css" rel="stylesheet">
    <link href="/layui/css/layui.css" rel="stylesheet">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/js/main.min.js"></script>
    <script type="text/javascript" src="/layui/layui.js"></script>
</head>
<body>
<div class="container-fluid p-t-15">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-toolbar clearfix">
                    <div class="toolbar-btn-action col-lg-12">
                        <div class="col-lg-9">
                            <form class="form-inline"  method="post" onsubmit="return false;">
                                <div class="form-group">
                                    <label class="sr-only" for="name">用户名</label>
                                    <input class="form-control" type="text" id="name" name="name" placeholder="请输入用户名">
                                </div>
                                <div class="form-group">
                                    <button class="btn btn-primary"  id="selectButton">查询</button>
                                </div>
                            </form>
                        </div>
                    </div>
                    <table class="table table-bordered" id="studentList" lay-filter="table1"></table>
                    <div id="page1"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<%--用户信息更新模板--%>
<div id="updateStudent" style="display: none">
    <form>
        <div class="form-group">
            <label>账号</label>
            <input class="form-control" type="text" id="studentNo" name="studentNo" readonly="true">
        </div>
        <div class="form-group">
            <label>用户姓名</label>
            <input class="form-control" type="text" id="studentName" name="studentName" >
        </div>
        <div class="form-group">
            <label>用户密码</label>
            <input class="form-control" type="text" id="studentPassword" name="studentPassword" >
        </div>
        <div class="form-group">
            <label>用户电话</label>
            <input class="form-control" type="text" id="studentPhone" name="studentPhone" >
        </div>
        <div class="form-group">
            <button class="btn btn-primary" type="submit" id="updateStudentButton">修改</button>
        </div>
    </form>
</div>
<script type="text/html" id="toolbar1">
    <div class="btn-group">
        <button class="btn btn-success m-r-5" lay-event="update">编辑</button>
        <button class="btn btn-danger" lay-event="delete">删除</button>
    </div>
</script>
<script type="text/html" id="typeConvert">
    <input type="checkbox" mid={{d.studentNo}} {{d.valid==true?"checked":""}} name="valid"
           lay-skin="switch"
           lay-filter="switchTest"
           lay-text="有效|无效">
</script>

<script type="text/javascript">
    $(function () {
        $.ajax({
            type:"post",
            url:"/dept/getAllDepart.action",
            data:{pid:0},
            dataType:"json",
            success:function (message) {
                var html="<option value=\"\">全部</option>";
                if (message.code==0){
                    alert("获取数据失败，请刷新页面重试");
                }else{
                    var depts=message.list;
                    for (var i = 0; i <depts.length ; i++) {
                        html+="<option value=\""+depts[i].id+"\">"+depts[i].pname+"</option>"
                    }
                    $("#xueyuan").html(html);
                }
            }
        })
    })
    function show2() {
        $.ajax({
            type:"post",
            url:"/dept/getAllDepart.action",
            data:{pid:$("#xueyuan").val()},
            dataType:"json",
            success:function (message) {
                var html="<option value=\"\">全部</option>";
                if (message.code==0){
                    alert("获取数据失败，请刷新页面重试");
                }else{
                    var depts=message.list;
                    for (var i = 0; i <depts.length ; i++) {
                        html+="<option value=\""+depts[i].id+"\">"+depts[i].pname+"</option>"
                    }
                    $("#banji").html(html);
                }
            }
        })
    }
    layui.use(["table","layer","form"],function () {
        var table=layui.table;
        var layer=layui.layer;
        var form=layui.form;
       var tableIns= table.render({
            elem:'#studentList',
            skin:'row,line',
            method:"post",
            url:'/student/selectAllStudent.action',
            limit:5,
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                ,limits:[5,10,15]
                ,first: '首页' //不显示首页
                ,last:"尾页" //不显示尾页
            },
            skin:'nob',// 无边框风格
            size:'lg',// 大尺寸
            done:function () {
                $(".layui-table-box").css("border-width","1px");
                $(".layui-table-header tr").css("height","50px");
                $(".layui-table-header tr td").css("width","50px");
                $(".layui-table-header tr").css("background-color","white");
                $(".layui-table-header tr span").css("color","#666666");
                $(".layui-table-body tr").css("height","50px");
            },
            cols:[[
                {field:'studentNo',title:'用户账号',sort:true,align:'center',width:140},
                {field:'studentName',title:'用户姓名',align:'center',width:180},
                {field:'password',title:'密码',sort:true,align:'center',width:160,templet:function (d) {
                        return '<p>******</p>'
                    }},
                {field:'studentPhone',title:'用户电话',sort:true,align:'center',width:160},
                {field:'validName',title:'是否有效',align:'center',templet: '#typeConvert'},
                {title:'操作',toolbar:"#toolbar1",align:'center'}
            ]]

        });
        table.on('tool(table1)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'delete'){ //删除
                layer.confirm('真的删除此用户吗，不是必须情况，请选择修改用户状态', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type:"post",
                        url:"/student/deleteStudent.action",
                        data:{studentNo:data.studentNo},
                        dataType:"json",
                        success:function (message) {
                            if (message.code==1){
                                layer.msg("删除成功")
                            }else{
                                layer.msg("删除失败")
                            }
                        }
                    })
                });
            } else if(layEvent === 'update') { //编辑
                $("#studentNo").val(data.studentNo);
                $("#studentName").val(data.studentName);
                $("#studentPassword").val(data.password);
                $("#studentPhone").val(data.studentPhone);
                //do something
                layer.open({
                    type:1,
                    title:"更新用户信息",
                    area: ['400px', '400px'],
                    content:$("#updateStudent")
                })
            }
        });
        form.on('switch(switchTest)', function(obj){
            // 通过属性获取绑定的id值
            var id = $(this).attr('mid');
            // 判断开关的状态
            var status=obj.elem.checked?true:false;
            //ajax请求方法
            $.ajax({
                url :"/student/updateValid.action",
                type : "post",
                dataType : "json",
                data : {studentNo:id,valid:status},
                success : function(data) {
                   if (data.code==1) {
                       layer.msg("修改成功")
                   }else{
                       layer.msg("修改失败")
                   }
                }
            });
        });
        $("#updateStudentButton").click(function () {
            $.ajax({
                type:"post",
                url:"/student/updateStudent.action",
                data:{id:$("#studentNo").val(),name:$("#studentName").val(),password:$("#studentPassword").val(),phone:$("#studentPhone").val() },
                dataType:"json",
                success:function (message) {
                    if (message.code==1){
                        layer.msg("修改成功");
                    }else{
                        layer.msg("修改失败");
                    }
                }
            })
        });
        $("#selectButton").click(function () {
            tableIns.reload({
                where:{
                    name:$("#name").val()
                },
                page:{curr:1}
            })
        })
        $("#addStudentButton").click(function () {
            layer.open({
                type:2,
                title:'添加用户',
                area: ['500px', '500px'],
                content:'/page/common/addStudent.jsp'
            })
        });
    })

</script>
</body>
</html>
